package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CateGoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : [wangtao]
 * @className : CategoryController
 * @description : []
 * @createTime : [2022/10/9 18:59]
 * @updateTime : [2022/10/9 18:59]
 */
@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {
@Autowired
private CateGoryService cateGoryService;


/*
新增菜品分类&新增套餐分类
 */
    @PostMapping
    public R<String> add(@RequestBody Category category){
        log.info("category:{}",category);
       cateGoryService.save(category);
        return R.success("新增分类成功");
    }
/*
* 菜品分类 分页
*
* */
    @GetMapping("/page")
public R PageInfo( int page,int pageSize ){
        log.info("CateGory: page:{},pageSize:{}",page,pageSize);
        Page<Category> ps = new Page(page, pageSize);
        LambdaQueryWrapper<Category> lqw=new LambdaQueryWrapper<>();
        lqw.orderByDesc(Category::getSort);
       cateGoryService.page(ps, lqw);
     return   R.success(ps);
}
    /*
     * 菜品分类 删除
     *
     * */
    @DeleteMapping
    public R<String> delete(long id) throws CustomException {
        cateGoryService.remove(id);
        return R.success("菜品分类删除成功");
    }

    /*
     * @author wangtao
     * @description 查询菜品分类列表
     * Function: listCategory
     * @param [type]
     * @return com.itheima.reggie.common.R<java.util.List<com.itheima.reggie.entity.Category>>
     * @createTime  2022/10/13 18:06
     */
    @GetMapping("/list")
    public R<List<Category>> listCategory(Category category){
        log.info("type = {}",category);
        LambdaQueryWrapper<Category> lqw=new LambdaQueryWrapper<>();
        Integer type = category.getType();
        lqw.eq(type!=null,Category::getType,type);
        lqw.orderByDesc(Category::getSort).orderByDesc(Category::getCreateTime);
        List<Category> Catelist = cateGoryService.list(lqw);

        System.out.println("CateGory CateGory CateGory CateGory CateGory CateGory v CateGory  CateGory CateGory  CateGory CateGory  CateGory"+Catelist);
        return R.success(Catelist);



    }


}
